package GameOntology;


import java.awt.*;
import java.util.ArrayList;

public abstract class Tank extends GameObject {
    //坦克速度
    private int speed=2;
    //坦克每个方向的图片地址
    private String tankUp;
    private String tankLeft;
    private String tankRight;
    private String tankDown;
    //坦克当前方向
    private String direction = "up";
    private String bulletImg;

    GameWindow gameWindow ;

    private boolean isMove=false;

    private boolean isAttack=false;

    private String team;

    public void setTeam(String team) {
        this.team = team;
        if (this.team.equals("computer")){
            this.direction="down";
        }
    }

    public String getTeam() {
        return team;
    }

    public void setBulletImg(String bulletImg) {
        this.bulletImg = bulletImg;
    }

    public boolean isMove() {
        return isMove;
    }

    public void setMove(boolean move) {
        isMove = move;
    }



    public Tank() {

    }

    public Tank( String img, int x, int y,
                String tankUp, String tankLeft,
                 String tankRight, String tankDown, GameWindow gameWindow) {
        super(60, 60, img, x, y);
        this.tankUp = tankUp;
        this.tankLeft = tankLeft;
        this.tankRight = tankRight;
        this.tankDown = tankDown;
        this.gameWindow = gameWindow;
    }

    public void setAttack(boolean attack) {
        isAttack = attack;
    }

    //移动
    public void move(String direction ,boolean isMove) {
        if (isMove) {
            int y = this.getY();
            int x = this.getX();
            this.direction = direction;
            switch (this.direction) {
                case "up":
                    this.setImg(tankUp);
                    if (collision(x,y-=speed)){
                        this.setY(y -= speed);
                        this.renewalRec(this.getX(),this.getY(),60,60);
                    }
                    break;
                case "left":
                    this.setImg(tankLeft);
                    if (collision(x-=speed,y)){
                        this.setX(x -= speed);
                        this.renewalRec(this.getX(),this.getY(),60,60);
                    }
                    break;
                case "right":
                    this.setImg(tankRight);
                    if (collision(x+=speed,y)){
                        this.setX(x += speed);
                        this.renewalRec(this.getX(),this.getY(),60,60);
                    }
                    break;
                case "down":
                    this.setImg(tankDown);
                    if (collision(x,y+=speed)){
                        this.setY(y += speed);
                        this.renewalRec(this.getX(),this.getY(),60,60);
                    }
                    break;
                default:
                    break;
            }
        }
    }
//找到坦克的"炮管"位置并添加子弹
    private Bullet getTankHead_Bullet(String img){
        switch (this.direction){
            case "up":
                return new Bullet(img,this.getX()+22,this.getY(),"up",this.team,gameWindow);
            case "down":
                return new Bullet(img,this.getX()+22,this.getY()+60,"down",this.team,gameWindow);
            case "right":
                return new Bullet(img,this.getX()+60,this.getY()+22,"right",this.team,gameWindow);
            case "left":
                return new Bullet(img,this.getX(),this.getY()+22,"left",this.team,gameWindow);
            default:return new Bullet();
        }
    }
//发射子弹
    public void attack(){
        if (isAttack){
            gameWindow.bullets.add(getTankHead_Bullet(this.bulletImg));
            isAttack = false;
        }
    }
    public boolean collision(int x,int y){
        ArrayList<Wall> walls = this.gameWindow.walls;
        Rectangle rectangle = new Rectangle(x,y,60,60);
        for (int i = 0; i < walls.size(); i++) {
            if (walls.get(i).getRec().intersects(rectangle)){
                return false;
            }
        }

        if (team.equals("player")){
            ArrayList<ComputerPlayer> computerPlayers = this.gameWindow.computerPlayers;
            for (int i = 0; i < computerPlayers.size(); i++) {
                if (computerPlayers.get(i).getRec().intersects(rectangle)){
                    return false;
                }
            }
        }

        if (x<0||x>913||y<0||y>730){
            return false;
        }
        return true;
    }
}
